package cl.scene.libbencode;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: Patricio
 * Date: 23-11-2010
 * Time: 09:48:44 PM
 */
public class Test {

    public static void main(String argv[]) {
        ByteArrayOutputStream ba = new ByteArrayOutputStream();
        BencodeParser parser = new BencodeParser();
        ListType root = new ListType();

        root.add(new IntegerValue(5894));
        root.add(new ListType() {
            @Override
            public void populate() {
                add(new ByteStringValue("Hola"));
                add(new ByteStringValue("Como"));
                add(new ByteStringValue("Estas"));
                add(new DictionaryType() {
                    @Override
                    public void populate() {
                        add(new ByteStringValue("Key1"), new ListType() {
                            @Override
                            public void populate() {
                                add(new IntegerValue(20));
                            }
                        });
                    }
                });
            }
        });

        root.add(new ByteStringValue("Chao"));

        try {
            root.encodeAndWrite(ba);
        } catch (IOException e) {
            e.printStackTrace();  
        }

        System.out.println(new String(ba.toByteArray()));

        try {
            parser.parse(new ByteArrayInputStream(ba.toByteArray()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

